 <!--
 * @Author: greatwwy
 * @Date: 2020-11-03 09:16:59
 * @LastEditTime: 2021-10-18 20:07:03
 * @LastEditors: jerry.zzy
 * @Description: In User Settings Edit
 * @FilePath: \zhcc-zhqh\src\views\chukuguanli\ChuKuShenQing.vue
-->

<template>
  <div>
    <template>
      <a-card>
        <!-- 顶部 -->
        <a-form-model ref="" :model="formdata" v-bind="layout">
          <a-row>
            <a-col :xl="6" :lg="6" :md="8" :sm="24" :xs="24">
              <a-form-model-item ref="" label="申请单号" prop="">
                <a-input
                  style="border: 0"
                  v-model="formdata.requisitionNo"
                  disabled
                >
                </a-input>
              </a-form-model-item>
            </a-col>
            <a-col :xl="6" :lg="6" :md="8" :sm="24" :xs="24">
              <a-form-model-item ref="" label="申请日期" prop="">
                <a-date-picker
                  v-model="formdata.applyDate"
                  value-format="YYYY-MM-DD"
                  @change="onChange"
                  style="width: 100%"
                />
              </a-form-model-item>
            </a-col>

            <a-col :xl="6" :lg="6" :md="8" :sm="24" :xs="24">
              <a-form-model-item ref="" label="申请仓库" prop="">
                <a-select
                  placeholder=""
                  v-model="formdata.shenqingWarehouse"
                  @change="setShenQingWarehouse"
                  showSearch
                >
                  <a-select-option
                    v-for="item in shenqingWarehouseList"
                    :key="item.depotId"
                    :value="item.depotNo + '|' + item.depotName"
                    >{{ item.depotNo }}{{ item.depotName }}
                  </a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>

            <a-col :xl="6" :lg="6" :md="8" :sm="24" :xs="24">
              <a-form-model-item ref="" label="申请部门" prop="">
                <a-tree-select
                  v-model="formdata.applicationDept"
                  style="width: 100%"
                  :tree-data="departmentList"
                  showSearch
                  @change="changeTreeNode"
                >
                </a-tree-select>
              </a-form-model-item>
            </a-col>
            <a-col :xl="6" :lg="6" :md="8" :sm="24" :xs="24">
              <a-form-model-item ref="" label="申请人" prop="">
                <a-input v-model="formdata.name" disabled></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :xl="6" :lg="6" :md="8" :sm="24" :xs="24">
              <a-form-model-item ref="" label="产成品仓库" prop="">
                <a-select
                  placeholder=""
                  v-model="formdata.productWarehouse"
                  @change="setProductWarehouse"
                  showSearch
                >
                  <a-select-option
                    v-for="item in productWarehouseList"
                    :key="item.depotId"
                    :value="item.depotNo + '|' + item.depotName"
                    >{{ item.depotNo }}{{ item.depotName }}
                  </a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :xl="6" :lg="6" :md="8" :sm="24" :xs="24">
              <a-form-model-item label="所属分厂" prop="ownedBranch">
                <a-tree-select
                  v-model="formdata.ownedBranch_text"
                  style="width: 100%"
                  :tree-data="ownedBranchList"
                  showSearch
                  @change="changeOwnedBranchTreeNode"
                >
                </a-tree-select>
              </a-form-model-item>
            </a-col>
            <a-col :xl="6" :lg="6" :md="12" :sm="24" :xs="24">
              <a-form-model-item label="产成品分类" prop="finishGoodsType">
                <a-tree-select
                  v-model="formdata.finishedProductType_text"
                  @change="setFinishGoodsType"
                  :tree-data="finishGoodsTypeList"
                  style="width: 100%"
                >
                </a-tree-select>
              </a-form-model-item>
            </a-col>
            <a-col :xl="6" :lg="6" :md="8" :sm="24" :xs="24">
              <a-form-model-item ref="" label="备注" prop="">
                <a-input v-model="formdata.mark"> </a-input>
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>

        <!-- 生成原单按钮组件 -->
        <planToApply
          @origin-data="getOriginData"
          @origin-data2="getOriginData2"
        ></planToApply>

        <a-table
          :columns="columns"
          :data-source="dataSource"
          :scroll="{ x: 3500 }"
          style="margin-top: 10px"
        >
          <!-- 仓库名称 -->
          <template slot="warehouse" slot-scope="text, record">
            <a-select
              placeholder=""
              v-model="record.warehouse"
              style="width: 100%"
              @change="setWarehouseGoodsList(record)"
              showSearch
            >
              <a-select-option
                v-for="item in warehouseList"
                :key="item.depotId"
                :value="
                  item.depotNo + '|' + item.depotName + '|' + item.depotId
                "
                >{{ item.depotName }}
              </a-select-option>
            </a-select>
          </template>
          <!-- 存货编码 -->
          <template slot="inventoryName" slot-scope="text, record">
            <a-select
              v-model="record.inventoryName"
              style="width: 100%"
              @change="setInventoryInfo(record.key)"
              @search="handleInventoryInfoSearch"
              showSearch
            >
              <a-select-option
                v-for="item in goodsList"
                :key="item.inventoryId"
                :value="
                  item.inventoryNo +
                  '|' +
                  item.inventoryName +
                  '|' +
                  item.standardModel +
                  '|' +
                  item.unit
                "
              >
                {{ item.inventoryNo }} {{ item.inventoryName }}
                {{ item.standardModel }}
              </a-select-option>
            </a-select>
          </template>

          <!-- 申请数量 -->
          <template slot="applyCount" slot-scope="text, record">
            <a-input-number
              v-model="record.applyCount"
              :precision="precision"
            ></a-input-number>
          </template>
          <!-- 实发数量 -->
          <!-- <template slot="reallyCount" slot-scope="text, record">
        <a-input-number v-model="record.reallyCount"></a-input-number>
      </template> -->
          <!-- 产成品分类 -->
          <template slot="finishGoodsType" slot-scope="text, record">
            <a-tree-select
              v-model="record.finishGoodsType"
              @change="setFinishGoodsType(record.key)"
              :tree-data="finishGoodsTypeList"
              style="width: 100%"
            >
            </a-tree-select>
          </template>
          <!-- 备注 -->
          <template slot="remarks" slot-scope="text, record">
            <a-input v-model="record.remarks"></a-input>
          </template>
          <template slot="operation" slot-scope="text, record">
            <span v-if="record.isNew">
              <a-popconfirm title="是否取消?" @confirm="remove(record.key)">
                <a>取消</a>
              </a-popconfirm>
            </span>
          </template>
        </a-table>
        <a-button
          style="width: 100%; margin-top: 16px; margin-bottom: 8px"
          type="dashed"
          icon="plus"
          @click="newMember"
          >添加</a-button
        >
        <a-descriptions
          bordered
          :column="{ xxl: 3, xl: 3, lg: 3, md: 3, sm: 1, xs: 1 }"
          style="text-align: right"
          size="small"
        >
          <a-descriptions-item
            v-for="(item, index) in label"
            :key="index"
            :label="item"
            :span="3"
          >
            <div style="text-align: left">
              <a-textarea
                style="background-color: white; color: black; border: 0"
                :auto-size="{ minRows: 4 }"
                disabled
              ></a-textarea>
            </div>

            <div>
              <a-row>
                <a-col :span="9"></a-col>
                <a-col :span="9">签名：</a-col>
                <a-col :span="6">日期：</a-col>
              </a-row>
            </div>
          </a-descriptions-item>
        </a-descriptions>
        <workflow
          :duty_name="duty_name"
          @submit-assignee="Submit"
          @reset-form="resetForm"
          ref="workflow"
        ></workflow>
      </a-card>
    </template>
  </div>
</template>

<script>
import moment from "moment";
import workflow from "@/components/common/workFlow";

import { ShenChanLingLiaoHTTP } from "@/services/modules/ChuKuGuanLi/ShengChanCaiLiaoChuKu/ShengChanLingLiao";
const $http = new ShenChanLingLiaoHTTP();

import planToApply from "../planToApply.vue"; //

import { CommonAjax } from "@/services/modules/common.js";
const comHttp = new CommonAjax(); // 公用接口

// import { ZiDianBiaoHttp } from "@/services/modules/ZiDianBiao.js";
// const zdbHttp = new ZiDianBiaoHttp(); // 公用接口
// 物品信息表格
const columns = [
  {
    title: "序号",
    dataIndex: "key",
    key: "key",
    align: "center",
  },

  {
    title: "仓库名称",
    dataIndex: "warehouse",
    key: "warehouse",
    align: "center",
    scopedSlots: { customRender: "warehouse" },
  },

  {
    title: "存货编码",
    dataIndex: "inventoryNo",
    key: "inventoryNo",
    align: "center",

    scopedSlots: { customRender: "inventoryNo" },
  },
  {
    title: "存货名称",
    dataIndex: "inventoryName",
    key: "inventoryName",
    align: "center",
    width: "12%",
    scopedSlots: { customRender: "inventoryName" },
  },
  {
    title: "规格型号",
    dataIndex: "specificationsModel",
    key: "specificationsModel",
    align: "center",
    scopedSlots: { customRender: "specificationsModel" },
  },

  {
    title: "单位",
    key: "unit",
    dataIndex: "unit",
    align: "center",
    scopedSlots: { customRender: "unit" },
  },
  {
    title: "申请数量",
    dataIndex: "applyCount",
    key: "applyCount",
    align: "center",
    scopedSlots: { customRender: "applyCount" },
  },
  {
    title: "实发数量",
    dataIndex: "reallyCount",
    key: "reallyCount",
    align: "center",
    scopedSlots: { customRender: "reallyCount" },
  },
  // {
  //   title: "产成品分类",
  //   key: "finishGoodsType",
  //   dataIndex: "finishGoodsType",
  //   align: "center",
  //   scopedSlots: { customRender: "finishGoodsType" },
  // },
  {
    title: "备注",
    key: "remarks",
    dataIndex: "remarks",
    align: "center",
    scopedSlots: { customRender: "remarks" },
  },

  {
    title: "操作",
    dataIndex: "operation",
    key: "operation",
    align: "center",
    scopedSlots: { customRender: "operation" },
  },
];
const dataSource = [];
export default {
  components: {
    workflow,
    planToApply,
  },
  data() {
    return {
      // 工作流意见
      label: ["分厂厂长意见", "仓库管理员意见"],
      formdata: {
        requisitionNo: "", //申请单号
        applyDate: moment().format("YYYY-MM-DD"), //申请日期
        affiliation: "", //所属仓库
        shenqingWarehouseNo: "", //申请仓库编码
        shenqingWarehouse: "", //申请仓库传Name
        shenqingWarehouseId: "", //申请仓库Id
        applicationDeptId: "",
        applicationDept: "",
        // applicationDeptId: this.$store.state.user.departments[0].id, //申请部门
        // applicationDept: this.$store.state.user.departments[0].departName, //申请部门传Name
        name: "", //申请人
        productWarehouse: "", //产成品仓库Name
        productWarehouseNo: "", //产成品仓库编码
        productWarehouseId: "", //产成品仓库Id
        mark: "", //备注

        finishedProductType_text: "", //产成品分类
        finishedProductType: "", //产成品分类
        ownedBranch_text: "", //所属分厂
        ownedBranch: "", //所属分厂
      },
      affiliationList: [], //所属仓库列表
      shenqingWarehouseList: [], //申请仓库下拉列表
      departmentList: [], //申请部门列表
      productWarehouseList: [], //产成品仓库列表
      // supplierList: [], //供应商列表
      storageUnitList: [], //存储单位列表
      // customerNameList: [], //供应商列表
      ownedBranchList: [], //所属分厂列表

      goodsList: [],

      // ===============表格中的列表数据======================
      warehouseList: [], //仓库名称列表
      finishGoodsTypeList: [], //产成品分类列表
      // goodsList: [], //物品列表
      // 格式
      layout: {
        labelCol: { span: 8 },
        wrapperCol: { span: 15 },
      },
      // 树
      value: "",

      visible: false,
      // ---------表格-----------
      columns, //列名
      dataSource,
      formTable: [
        // "affiliation",
        // "warehouse",
        "inventoryNo",
        "inventoryName",
        "specificationsModel",
        "unit",
        "applyCount",
        "reallyCount",
        "sourceNumber",
        "finishGoodsType",
        "remark",
      ],
      duty_name: "生产领料流程",
      precision: 3, //销售系统保留3位小数
    };
  },
  watch: {
    $route(to, from) {
      //监听路由是否变化
      console.log("to", to);
      let id = to.query.Id;
      console.log("from", from);
      console.log("this.$route.params.articleId", this.$route.params.articleId);
      this.setInitInfo(id);
    },
  },
  // 生命周期函数
  mounted() {
    this.judgeDocument(); //判断单据号是否为空
    this.setColumns();
    this.getApplyOrder(); //获取申请单号
    this.getWarehouseInfo(); //申请仓库列表
    this.getDeparmentTree(); //申请部门列表
    // this.getAffiliationListInfo(); //所属仓库列表
    this.getUserByToken(); //申请人
    this.getProductWarehouseInfo(); //产成品仓库
    // this.getSupplierInfo(); //供应商列表
    this.getwarehouseInfo(); //获取仓库名称列表
    this.getFinishGoodsTypeList(); //获取产成品分类列表
    this.sendDutyName(); // 工作流方法
    this.getownedBranchTree(); //获取分厂列表
  },
  methods: {
    /**
      //todo @description: 2.0功能
      * @description: ...
      * @return
      */
    getOriginData(data) {
      console.log(data);
    },
    getOriginData2(data) {
      console.log("传入的存货信息:::::", data);

      this.dataSource = data;
      data.forEach((item) => {
        item.applyCount = item.applyNumber;
        item.standardModel = item.specifications;
      });
    },

    // ————判断单据号是否为空————
    judgeDocument() {
      setTimeout(() => {
        if (this.formdata.requisitionNo === "") {
          this.$message.error("未获取到单据号,请刷新页面重试");
        }
      }, 3000);
    },
    // 获取分厂列表
    getownedBranchTree() {
      comHttp.getDepartmentList().then((res) => {
        // let tree = this.resetTree(res.result);
        console.log("获取部门树+++++++++++::::", res);
        this.ownedBranchList = res.result;
        this.changeownedBranchData(this.ownedBranchList);
      });
    },
    // 改变用友传递过来的数据为组件能识别的数据
    changeownedBranchData(Tree) {
      Tree.forEach((element) => {
        element["title"] = element.deptname;
        element["children"] = element.syncDepartmentFilesEntityList;
        element["key"] = element.pkDeptdoc;
        element["value"] = element.pkDeptdoc;

        if (element.children) {
          this.changeownedBranchData(element.children);
        }
      });
    },
    // 部门下拉框选择
    changeOwnedBranchTreeNode(key, value) {
      console.log("key", key);
      console.log("value", value);
      this.formdata.ownedBranch_text = value[0];
      this.formdata.ownedBranch = key;
      console.log("选择的值::::", this.formdata.ownedBranch);
      console.log("ownedBranchId::::", this.formdata.ownedBranchId);
    },
    // 客户模糊搜索
    searchCustomer(value) {
      if (value == "") {
        this.$message.error("请输入客户名称！");
      } else if (value == " ") {
        value = "";
        console.log("value", value);
        this.$message.error("请输入真实数据！");
      } else {
        this.customerNameList = [];
        let data = "?customerName=" + value + "&custflag=3,1";
        comHttp.getCustomerInfoByName(data).then((res) => {
          console.log("res", res);
          for (let i = 0; i < res.result.list.length; i++) {
            let data = {
              id: res.result.list[i].pkCumandoc,
              customerCode: res.result.list[i].custcode,
              name: res.result.list[i].custname,
            };
            this.customerNameList.push(data);
          }
        });
      }
    },
    // 客户选择的处理函数
    changeCustomer(value) {
      console.log("客户选择处理函数", value);
      let array = value.split("|");
      this.formdata.supplierCode = array[0]; //客户编码
      this.formdata.supplier = array[1]; //客户名称
      this.formdata.supplierId = array[2]; //客户Id
      console.log("this.form.customerId", this.formdata.customerId);
      let id = array[2]; //客户id
      console.log("id", id);
    },
    //!日期默认当天
    // 默认显示当天日期
    moment,
    getCurrentData() {
      return new Date().toLocaleDateString();
    },

    // !下拉框搜索
    async handleInventoryInfoSearch(value) {
      var goodsList_invname = [];
      var goodsList_invspec = [];
      this.goodsList = [];
      if (value == "") {
        this.$message.error("请输入物品名称！");
      } else if (value == " ") {
        value = "";
        console.log("value", value);
        this.$message.error("请输入真实数据！");
      } else {
        console.log("value!!!!!", value);
        let queryPath = "?name=" + value;
        await comHttp
          .getSearchInventoryInformationFile(queryPath)
          .then((res) => {
            console.log("res", res.result);
            for (let i = 0; i < res.result.length; i++) {
              let data = {
                inventoryId: res.result[i].pkInvbasdoc, // 存货 id
                inventoryName: res.result[i].invname,
                inventoryNo: res.result[i].invcode,
                standardModel: res.result[i].invspec,
                unit: res.result[i].measname,
              };
              if (data.standardModel == null) {
                data.standardModel = "";
              }
              if (data.unit == undefined) {
                data.unit = "";
              }
              goodsList_invname.push(data);
            }
          });

        let queryInventoryBySpec = "?invspec=" + value;
        //规格型号模糊查询
        await comHttp
          .getqueryInventoryBySpec(queryInventoryBySpec)
          .then((res) => {
            console.log("结果规格型号模糊查询", res);

            goodsList_invspec = res.result.list.map((item) => {
              return {
                inventoryId: item.pkInvbasdoc,
                inventoryNo: item.invcode,
                inventoryName: item.invname,
                standardModel: item.invspec,
                unit: item.measname,
              };
            });
          });
        //todo 合并去重函数
        await this.mergeArray(goodsList_invname, goodsList_invspec);
      }
    },
    //todo 合并去重函数
    mergeArray(arr1, arr2) {
      let arrs = [...arr1, ...arr2];

      //根据inventoryId去重
      let map = new Map();
      for (let item of arrs) {
        if (!map.has(item.inventoryId)) {
          map.set(item.inventoryId, item);
        }
      }
      this.goodsList = [...map.values()];
    },
    // 所属仓库列表
    getAffiliationListInfo() {
      $http.getWarehouseInfo("").then((res) => {
        console.log("所属仓库列表", res);
        for (let i = 0; i < res.result.length; i++) {
          let data = {
            id: res.result[i].pkStordoc, //所属仓库id
            depotName: res.result[i].storname, //所属仓库名称
            depotNo: res.result[i].storcode, //所属仓库 编码
          };
          this.affiliationList.push(data);
        }
      });
    },

    // 选择表头中的所属仓库后设置表格中的所属仓库
    setAffiliation(value, option) {
      console.log(value);
      console.log("option-----------》", option);
      let array = [];
      array = value.split("|");
      // console.log("array==========>", array);
      this.formdata.affiliation = array[1];
      let key = option.key;
      for (let i = 0; i < this.dataSource.length; i++) {
        this.dataSource[i].affiliation = this.formdata.affiliation;

        this.dataSource[i].affiliationId = key;

        // this.setWarehouseGoodsList(key);
      }
    },

    // 申请仓库列表
    getWarehouseInfo() {
      $http.getWarehouseInfo("?pageSize = 10000").then((res) => {
        console.log("申请仓库列表", res);
        for (let i = 0; i < res.result.length; i++) {
          let data = {
            depotId: res.result[i].pkStordoc,
            depotName: res.result[i].storname,
            depotNo: res.result[i].storcode,
          };
          this.shenqingWarehouseList.push(data);
        }
      });
    },

    // 选择申请仓库后设置表格中的申请仓库，并渲染该仓库下拥有的物品
    setShenQingWarehouse(value, option) {
      console.log(value);
      console.log("option!!!!!!", option);
      let array = [];
      array = value.split("|");
      this.formdata.shenqingWarehouse = array[1];
      this.formdata.shenqingWarehouseNo = array[0];
      this.formdata.shenqingWarehouseId = option.key;

      for (let i = 0; i < this.dataSource.length; i++) {
        this.dataSource[i].warehouse = this.formdata.shenqingWarehouse;
        this.dataSource[i].warehouseNo = this.formdata.shenqingWarehouseNo;
        this.dataSource[i].warehouseId = this.formdata.shenqingWarehouseId;
        this.dataSource[i].goodsList = this.getGoodSInfo(option.key);
      }
    },

    // 获取物品信息,后期可能会修改为根据仓库id获取物品信息，因为物品信息是根据仓库id来的！！！！！
    getGoodSInfo(id) {
      let goodsList = [];
      comHttp.getInventoryByDepotId("?id=" + id).then((res) => {
        console.log("获取存货信息:::::", res);
        for (let i = 0; i < res.result.length; i++) {
          let data = {
            inventoryId: res.result[i].id, // 存货 id
            inventoryName: res.result[i].inventoryName,
            inventoryNo: res.result[i].inventoryNo,
            standardModel: res.result[i].specificationsModel,
            unit: res.result[i].unit,
          };
          goodsList.push(data);
        }
      });
      return goodsList;
    },
    // ==============表格中函数开始================
    // 默认设置五行，让用户方便添加
    setColumns() {
      console.log("this.$store", this.$store.state);
      let size = 1;
      this.dataSource = [];

      for (let i = 0; i < size; i++) {
        let data = {
          key: this.dataSource.length + 1,
          isNew: true,
          affiliation: "", //所属仓库
          affiliationId: "", //所属仓库Id
          warehouse: "", //仓库名称
          warehouseNo: "", //仓库编码
          warehouseId: "", //仓库编码传id
          inventoryNo: "", //存货编码
          inventoryName: "", //存货名称
          specificationsModel: "", //规格型号
          unit: "", //单位
          applyCount: "", //申请数量
          reallyCount: "", //实发数量
          finishGoodsType: "", //产成品分类
          finishGoodsTypeId: "", //产成品分类ID
          remarks: "", //备注
          // farmers: "", //供应商Id
          // farmers_text: "", //供应商Name
          finishedProductDepot: "", //产成品仓库Id
          finishedProductDepot_text: "", //产成品仓库Name
          goodsList: [], //物品列表
        };
        this.dataSource.push(data);
      }
      // console.log("默认加载的数据::::",this.DataSource)
    },

    // 表格添加新的一条记录
    newMember() {
      this.dataSource.push({
        key: this.dataSource.length + 1,
        isNew: true,
        affiliation: "", //所属仓库
        affiliationId: "", //所属仓库Id
        warehouse: "", //仓库名称
        warehouseNo: "", //仓库编码
        warehouseId: "", //仓库编码传id
        inventoryNo: "", //存货编码
        inventoryName: "", //存货名称
        specificationsModel: "", //规格型号
        unit: "", //单位
        applyCount: "", //申请数量
        reallyCount: "", //实发数量
        finishGoodsType: "", //产成品分类
        finishGoodsTypeId: "", //产成品分类ID
        remark: "", //备注
        // farmers: "", //供应商Id
        // farmers_text: "", //供应商Name
        finishedProductDepot: "", //产成品仓库Id
        finishedProductDepot_text: "", //产成品仓库Name
        goodsList: [], //物品列表
      });
    },

    // 表格取消一行
    remove(key) {
      for (let i = 0; i < this.dataSource.length; i++) {
        if (this.dataSource[i].key == key) {
          if (this.dataSource.length > 1) {
            this.dataSource.splice(key - 1, 1);

            this.$message.success("取消成功");
          } else {
            this.$message.error("至少保留一行");
          }
        }
        this.dataSource[i].key = i + 1;
      }
    },
    // 所属仓库的change函数
    changeAffiliation(record) {
      console.log(record, 44444444444);
      let array = record.affiliation.split("|");
      record.affiliationNo = array[0];
      record.affiliation = array[1];
      record.affiliationId = array[2];
    },
    // 仓库名称列表
    getwarehouseInfo() {
      $http.getWarehouseInfo("").then((res) => {
        console.log("仓库名称列表", res);
        for (let i = 0; i < res.result.length; i++) {
          let data = {
            depotId: res.result[i].pkStordoc,
            depotName: res.result[i].storname,
            depotNo: res.result[i].storcode,
          };
          this.warehouseList.push(data);
        }
      });
    },

    // 根据选择的仓库确定该仓库下对应的存货
    setWarehouseGoodsList(record) {
      console.log("record----->", record);
      let array = record.warehouse.split("|");
      record.warehouseNo = array[0];
      record.warehouse = array[1];
      record.warehouseId = array[2];
      record.goodsList = this.getGoodSInfo(array[2]);
      console.log("goodsList", record.goodsList);
      // console.log("storageInfo", storageInfo);
    },

    // 填充物品名称，规格，单位
    setInventoryInfo(key) {
      console.log("value->", key);
      let target = this.dataSource.filter((item) => item.key === key)[0];
      console.log("target->", target);
      let array = target.inventoryName.split("|");
      console.log("array", array);
      target.inventoryNo = array[0]; // 赋值给存货编码
      target.inventoryName = array[1]; // 赋值给存货名称
      target.specificationsModel = array[2]; //赋值规格型号
      target.unit = array[3]; //赋值单位
    },

    // 获取产成品分类列表
    getFinishGoodsTypeList() {
      comHttp.GetSaveCargoClassification().then((res) => {
        console.log("产成品分类列表", res);

        this.finishGoodsTypeList = res.result;
        this.changeFinishGoodsType(this.finishGoodsTypeList);
      });
    },
    // 改变用友传递过来的数据为组件能识别的数据
    changeFinishGoodsType(Tree) {
      Tree.forEach((element) => {
        element["title"] = element.invclassname;
        element["children"] = element.syncInventoryClassificationEntityList;
        element["key"] = element.pkInvcl + "|" + element.invclassname;
        element["value"] = element.pkInvcl + "|" + element.invclassname;

        if (element.children) {
          this.changeFinishGoodsType(element.children);
        }
      });
    },

    // 处理产成品分类
    setFinishGoodsType(key, value) {
      console.log("key======>", key);
      console.log("value======>", value);
      let array = key.split("|");
      this.formdata.finishedProductType = array[0];
      this.formdata.finishedProductType_text = value;
      console.log(this.formdata.finishedProductType, 22222222222222);
      console.log(this.formdata.finishedProductType_text, 33333333);
    },
    // ==============表格中函数结束================

    // 获取单号
    getApplyOrder() {
      comHttp.getOrder("?string=" + "SCSQ-").then((res) => {
        console.log("获取的单号=======》", res);
        this.formdata.requisitionNo = res.message;
      });
    },

    // 获取部门列表
    getDeparmentTree() {
      comHttp.getDepartmentList().then((res) => {
        // let tree = this.resetTree(res.result);
        console.log("获取部门树::::", res);
        this.departmentList = res.result;
        this.changeCorpData(this.departmentList);
      });
    },
    // 改变用友传递过来的数据为组件能识别的数据
    changeCorpData(Tree) {
      Tree.forEach((element) => {
        element["title"] = element.deptname;
        element["children"] = element.syncDepartmentFilesEntityList;
        element["key"] = element.pkDeptdoc;
        element["value"] = element.pkDeptdoc;

        if (element.children) {
          this.changeCorpData(element.children);
        }
      });
    },
    // 部门下拉框选择
    changeTreeNode(key, value) {
      console.log("key", key);
      console.log("key", value);
      this.formdata.applicationDeptId = key;
      this.formdata.applicationDept = value[0];
      console.log("选择的值::::", this.formdata.applicationDept);
      console.log(
        "选择的applicationDeptId值::::",
        this.formdata.applicationDeptId
      );
    },

    //  根据TOKEN获取登录人的部分信息
    getUserByToken(queryPath = "") {
      comHttp.getUserByToken(queryPath).then((res) => {
        console.log("res", res);
        this.formdata.id = res.result.sysUserId;
        this.formdata.name = res.result.sysUsername;
        console.log("this.formdata.name", this.formdata.name);
        this.getDeptByUserId(this.formdata.id).then((res) => {
          this.formdata.applicationDept = res.accountingDept_dictText;
          this.formdata.applicationDeptId = res.accountingDept;
          console.log(
            "this.formdata.applicationDepart",
            this.formdata.applicationDept
          );
        });
      });
    },
    // 根据用户获取该用户的部门
    getDeptByUserId(userId) {
      let queryPath = "?userId=" + userId;
      return new Promise((resolve) => {
        comHttp.getDeptByUserId(queryPath).then((res) => {
          console.log("res++++userId+", res);
          let accountingDeptID = res.result.object;
          resolve(accountingDeptID);
          // this.getUserByDeptId(accountingDeptID);
        });
      });
    },
    // 产成品仓库列表
    getProductWarehouseInfo() {
      $http.getWarehouseInfo("").then((res) => {
        console.log("产成品仓库列表", res);
        for (let i = 0; i < res.result.length; i++) {
          let data = {
            depotId: res.result[i].pkStordoc,
            depotName: res.result[i].storname,
            depotNo: res.result[i].storcode,
          };
          this.productWarehouseList.push(data);
        }
      });
    },
    // 设置产成品仓库函数
    setProductWarehouse(value, option) {
      console.log(value);
      console.log("option-----------》", option);
      let array = [];
      array = value.split("|");
      this.formdata.productWarehouseNo = array[0];
      this.formdata.productWarehouse = array[1];
      this.formdata.productWarehouseId = option.key;
      for (let i = 0; i < this.dataSource.length; i++) {
        this.dataSource[i].finishedProductDepot =
          this.formdata.productWarehouseId;
        this.dataSource[i].finishedProductDepot_text =
          this.formdata.productWarehouse;
      }
      // console.log("thsi.dataSource", this.dataSource);
    },
    // 获取供应商列表
    // getSupplierInfo(queryPath = "") {
    //   comHttp.getSupplierInfo(queryPath).then((res) => {
    //     console.log("供应商列表-》》》", res);
    //     for (let i = 0; i < res.result.length; i++) {
    //       let data = {
    //         supplierId: res.result[i].id,
    //         suppilerCode: res.result[i].suppilerCode,
    //         suppilerName: res.result[i].suppilerName,
    //       };
    //       this.supplierList.push(data);
    //     }
    //   });
    // },

    // 处理供应商列表
    // handleSupplierInfo(value, option) {
    //   console.log("value==========>", value);
    //   let array = value.split("|");
    //   console.log("array======>", array);
    //   this.formdata.supplierCode = array[0];
    //   this.formdata.supplier = array[1];
    //   this.formdata.supplierId = option.key;
    //   console.log("this.formdata.supplier", this.formdata.supplierId);
    //   for (let i = 0; i < this.dataSource.length; i++) {
    //     this.dataSource[i].farmers = this.formdata.supplierId;
    //     this.dataSource[i].farmers_text = this.formdata.supplier;
    //   }
    //   // console.log("thsi.dataSource", this.dataSource);
    // },
    onChange(date, dateString) {
      console.log(date, dateString);
    },

    // 表格选择事件
    onSelectChange(selectedRowKeys) {
      console.log("selectedRowKeys changed: ", selectedRowKeys);
      this.selectedRowKeys = selectedRowKeys;
    },

    // 工作流
    sendDutyName() {
      this.$refs.workflow.getProcessName();
    },

    // 发送按钮
    Submit(queryPath) {
      if (
        this.formdata.affiliation == "undefined" ||
        this.formdata.affiliation == "null"
      ) {
        this.formdata.affiliation == "";
      }
      if (
        this.formdata.shenqingWarehouse == "undefined" ||
        this.formdata.shenqingWarehouse == "null"
      ) {
        this.formdata.shenqingWarehouse == "";
      }
      if (
        this.formdata.productWarehouse == "undefined" ||
        this.formdata.productWarehouse == "null" ||
        this.formdata.productWarehouse == ""
      ) {
        // this.formdata.productWarehouse == "";
        this.$message.error("请选择产成品仓库！");
      }
      if (
        this.formdata.ownedBranch_text == "undefined" ||
        this.formdata.ownedBranch_text == "null" ||
        this.formdata.ownedBranch_text == ""
      ) {
        this.$message.error("请选择所属分厂！");
      }
      if (
        this.formdata.finishedProductType_text == "undefined" ||
        this.formdata.finishedProductType_text == "null" ||
        this.formdata.finishedProductType_text == ""
      ) {
        this.$message.error("请选择产成品分类！");
      } else {
        let path =
          queryPath +
          "&requisitionNo=" +
          this.formdata.requisitionNo +
          "&applicationDate=" +
          this.formdata.applyDate +
          "&subordinate_text=" +
          this.formdata.affiliation +
          "&applicationDepot=" +
          this.formdata.shenqingWarehouseId +
          "&applicationDepot_text=" +
          this.formdata.shenqingWarehouse +
          "&applicationDept=" +
          this.formdata.applicationDeptId +
          "&applicationDept_text=" +
          this.formdata.applicationDept +
          "&applicant=" +
          this.formdata.id +
          "&applicantId_text=" +
          this.formdata.name +
          "&finishedProductDepot=" +
          this.formdata.productWarehouseId +
          "&finishedProductDepot_text=" +
          this.formdata.productWarehouse +
          "&remarks=" +
          this.formdata.mark +
          "&sign=0" +
          "&mark=0" +
          "&ownedBranch=" +
          this.formdata.ownedBranch +
          "&ownedBranch_text=" +
          this.formdata.ownedBranch_text +
          "&finishedProductType=" +
          this.formdata.finishedProductType +
          "&finishedProductType_text=" +
          this.formdata.finishedProductType_text;
        // "&documentClosed=开启";
        console.log("path", path);
        // 将存货编码为空的的数据过滤掉
        // const newData = this.dataSource.filter(
        //   (item) => item.inventoryNo != ""
        // );
        // console.log("过滤好的数据::::", newData);
        const newData = this.dataSource;

        let array = [];

        for (let i = 0; i < newData.length; i++) {
          if (
            newData[i].warehouse == "undefined" ||
            newData[i].warehouse == "null" ||
            newData[i].warehouse == ""
          ) {
            this.$message.error("请选择仓库名称！");
          } else if (
            newData[i].inventoryNo == "undefined" ||
            newData[i].inventoryNo == "null" ||
            newData[i].inventoryNo == ""
          ) {
            this.$message.error("请选择存货！");
          } else if (
            newData[i].applyCount == "undefined" ||
            newData[i].applyCount == "null" ||
            newData[i].applyCount == ""
          ) {
            this.$message.error("请填写申请数量！");
          } else {
            let data2 = {
              storageUnit: newData[i].affiliationId, //所属仓库ID
              subordinate_text: newData[i].affiliation, //所属仓库传name
              depotId: newData[i].warehouseId, //仓库名称传Id
              applicationDepot_text: newData[i].warehouse, //仓库名称传name
              inventoryNo: newData[i].inventoryNo, //存货编码
              inventoryNane: newData[i].inventoryName, //存货名称
              standardModel: newData[i].specificationsModel, //规格型号
              unit: newData[i].unit, //单位
              applyNumber: newData[i].applyCount, //申请数量
              actualQuantity: newData[i].reallyCount, //实发数量
              finishedProductType: newData[i].finishGoodsTypeId, //产成品分类传Id
              finishedProductType_text: newData[i].finishGoodsType, //产成品分类传Name
              finishedProductDepot: newData[i].finishedProductDepot, //产成品仓库Id
              finishedProductDepot_text: newData[i].finishedProductDepot_text, //产成品仓库Name
              remarks: newData[i].remarks, //备注
              // documentClosed: "开启", //单据状态
            };
            array.push(data2);
            console.log("array========>", array);
            console.log("我要发送！！！！！！！！！！！！", array);
            console.log("queryPath======>", queryPath);
          }
        }
        if (array.length == 0) {
          this.$message.error("请填写存货信息！");
        } else {
          $http.saveShengChanLingLiao(path, array).then((res) => {
            if (res.code == 200) {
              this.$message.success("提交成功！");
              this.resetForm();
              // this.getApplyOrder(); //获取申请单号
              // this.getUserByToken(); //获取申请人
              // this.setColumns();
            } else {
              this.$message.error("提交失败！");
            }
          });
        }
      }
      // let array = [];
    },
    // 重置按钮
    resetForm() {
      this.formdata = {
        requisitionNo: "",
        applyDate: moment().format("YYYY-MM-DD"), //申请日期
        affiliation: "", //所属仓库
        shenqingWarehouseNo: "", //申请仓库编码
        shenqingWarehouse: "", //申请仓库传Name
        shenqingWarehouseId: "", //申请仓库Id

        productWarehouse: "", //产成品仓库Name
        productWarehouseNo: "", //产成品仓库编码
        productWarehouseId: "", //产成品仓库Id
        applicationDept: "",
        applicationDeptId: "",
        mark: "", //备注
      };

      this.dataSource = [];
      this.getApplyOrder(); //获取申请单号
      this.getUserByToken();
      this.setColumns();

      this.$refs.workflow.ResetASSIGNEE();
    },
  },
};
</script>
<style></style>
